package hopital.sql;

import java.sql.ResultSet;
import java.util.List;

public abstract class SqlObject {

    public abstract SqlQuery insert();

    public abstract SqlQuery update();

    public abstract SqlQuery delete();

    public abstract SqlQuery select();

    public abstract List<Object> getTable(ResultSet _rs) throws Exception ;

    public List<Object> retrive(SqlDatabase _db) throws Exception {
        return getTable(_db.doQuery(select().getQuery(), select().getParameters()));
    }

    public boolean destroy(SqlDatabase _db) throws Exception {
        return _db.doNonQuery(delete().getQuery(), delete().getParameters());
    }

    public boolean save(SqlDatabase _db) throws Exception {
        if (getId() != -1) {
            return _db.doNonQuery(update().getQuery(), update().getParameters());
        } else {
            return _db.doNonQuery(insert().getQuery(), insert().getParameters());
        }
    }

    protected int _id=-1;

    public int getId() {
        return _id;
    }

    public void setId(int _id) {
        this._id = _id;
    }

}
